package learnjava.basic.other;

import java.io.*;
;

public class TestIO {

    public static void main(String[] args) {
//        testByteArrayStream();
    }

    public static void testByteArrayStream(){
        ByteArrayInputStream bis = new ByteArrayInputStream("abcdwef0".getBytes());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int res = 0;
        while ((res=bis.read())!=-1){
            bos.write(res);//将bis的输入读到bos内部的可变长buffer数组
        }
        System.out.println(bos.toString());  //abcdwef0
        ByteArrayInputStream bis1 = new ByteArrayInputStream("fergergv".getBytes());
        while ((res=bis1.read())!=-1){
            bos.write(res);//测试bos内部的buffer数组是否可变长
        }
        System.out.println(bos.toString());  //abcdwef0fergergv
        //总结来说，ByteArrayOutputStream存在的意义就是封装了一个可变长的byte数组，可以接收长度不同的byte数组
    }

    public static void test1() throws Exception{

//        FileOutputStream fos = new FileOutputStream("d:\\out.txt");
//        FileInputStream fis = new FileInputStream("d:\\in.txt");
//        int res;
//        while((res=fis.read())!=-1){
//            System.out.println(res);//两个整数代表一个汉字，一个整数代表一个字符
//            fos.write(res);
//        }
//        fis.close();
//        fos.close();

//        FileWriter fw = new FileWriter("d:\\out.txt");
//        FileReader fr = new FileReader("d:\\in.txt");
//        int res = 0;
//        while((res=fr.read())!=-1){
//            System.out.println((char)res);
//            fw.write(res);
//        }
//        for (int i = 65; i < 65535; i++) {
//            System.out.println((char)i);
//            fw.write(i);
//            if(i%100==0) fw.write('\n');
//        }
//        fw.close();
//        fr.close();


        //从某一行开始读文件
//        LineNumberReader lnr = new LineNumberReader(new FileReader("E:\\java_workspace\\foroffer\\src\\learnjava\\basic\\day3_29.java"));
//        String line = null;
//        lnr.setLineNumber(5);
//        while ((line=lnr.readLine())!=null&&lnr.getLineNumber()<20){
//            System.out.println(lnr.getLineNumber()+"::::"+line);
//        }
//        lnr.close();

        DataInputStream mDataInputStream = new DataInputStream(new FileInputStream("C:\\Users\\wangc\\Desktop\\ADS1292DATA_002.bin"));
        int read = 0;
        for (int i = 0; i < 100; i++) {
            read = mDataInputStream.readShort();
            System.out.println(Integer.toHexString(read)+"   "+read);
            read = mDataInputStream.readShort();
            System.out.println(Integer.toHexString(read)+"   "+read);
            read = mDataInputStream.readShort();
            System.out.println(Integer.toHexString(read)+"   "+read);
            read = mDataInputStream.readShort();
            System.out.println(Integer.toHexString(read)+"   "+read);

            read = mDataInputStream.readInt();
            //第24位是符号位，判断正负
            int sign = read>>>23==0?1:-1;
            read = sign * (read&0x7FFFFF);
            System.out.println(Integer.toHexString(read)+"   "+read);

            read = mDataInputStream.readInt();
            sign = read>>>23==0?1:-1;
            read = sign * (read&0x7FFFFF);
            System.out.println(Integer.toHexString(read)+"   "+read);
        }
    }
}
